<?php
include'Utils.php';
class MainInformationsController extends AppController{
	
	public $components = array('CenterInfo');
	
	/**
	 * Admin function used to display index page
	 */
	
	public function index(){
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get common information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);
		
		$isBack = (isset($_POST['isBack'])) ? $_POST['isBack'] : "";
		if($isBack == ""){
			// Get information from database
			$infos = $this->MainInformation->findAllBym_mif_id(1);
			$this->set("infomation", $infos[0]);
			$this->set("isBack", $isBack);
		}else{
			// Get information from confirm page
			$title = (isset($_POST['title'])) ? $_POST['title'] : "";
			$info = (isset($_POST['info'])) ? $_POST['info'] : "";
			$this->set('title', $title);
			$this->set('info', $info);
			$this->set('isBack', $isBack);
		}
	}
	
	/**
	 * Admin function used to display confirm page
	 */
	public function confirm(){
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get common information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);
		
		// Get POST data
		$title = (isset($_POST['title'])) ? htmlentities($_POST['title'], ENT_QUOTES, 'utf-8') : "";
		$info = (isset($_POST['info'])) ? htmlentities($_POST['info'], ENT_QUOTES, 'utf-8') : "";

		// Set data used to display
		$this->set("title", $title);
		$this->set("info", $info);
		
	}
	
	/**
	 * Admin function used to regist data into data base
	 */
	public function regist(){
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Get common information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);
		
		// Get POST data
		$title = isset($_POST['title']) ? htmlentities($_POST['title'], ENT_QUOTES, 'utf-8') : "";
		$info = isset($_POST['info']) ? htmlentities($_POST['info'], ENT_QUOTES, 'utf-8') : "";
		
		// Create saved array data
		$savedData = array(
			'MainInformation.m_mif_slogan' => "'".$title. "'",
			'MainInformation.m_mif_construction' => "'". $info . "'"
		);
		
		// Create update condition
		$condition = array(
			'MainInformation.m_mif_id' => 1
		);
		
		// Regist data into database
		$this->MainInformation->updateAll($savedData, $condition);
		
		// Display index page
		$this->redirect(array('action' => 'index', null));
		
	}
	
}	
?>